<template>
  <div>
    <el-form-item label="能否搜索">
      <el-switch v-model="activeData.filterable" />
    </el-form-item>
    <el-form-item label="是否多选">
      <el-switch v-model="activeData.multiple" @change="multipleChange" />
    </el-form-item>
    <template>
      <el-divider>选项</el-divider>
      <el-form-item v-if="activeData.__config__.dataType" label="数据类型">
        <el-radio-group v-model="activeData.__config__.dataType" size="small">
          <el-radio-button label="dynamic"> 动态数据 </el-radio-button>
          <el-radio-button label="static"> 静态数据 </el-radio-button>
        </el-radio-group>
      </el-form-item>

      <template v-if="activeData.__config__.dataType === 'dynamic'">
        <el-form-item label="接口地址">
          <el-input
            v-model="activeData.__config__.url"
            :title="activeData.__config__.url"
            placeholder="请输入接口地址"
            clearable
            @blur="$emit('fetch-data', activeData)"
          >
            <el-select
              slot="prepend"
              v-model="activeData.__config__.method"
              :style="{ width: '85px' }"
              @change="$emit('fetch-data', activeData)"
            >
              <el-option label="get" value="get" />
              <el-option label="post" value="post" />
              <el-option label="put" value="put" />
              <el-option label="delete" value="delete" />
            </el-select>
          </el-input>
        </el-form-item>
        <el-form-item label="数据位置">
          <el-input
            v-model="activeData.__config__.dataPath"
            placeholder="请输入数据位置"
            @blur="$emit('fetch-data', activeData)"
          />
        </el-form-item>

        <template v-if="activeData.props && activeData.props.props">
          <el-form-item label="标签键名">
            <el-input
              v-model="activeData.props.props.label"
              placeholder="请输入标签键名"
            />
          </el-form-item>
          <el-form-item label="值键名">
            <el-input
              v-model="activeData.props.props.value"
              placeholder="请输入值键名"
            />
          </el-form-item>
          <el-form-item v-if="activeData.props.props.children" label="子级键名">
            <el-input
              v-model="activeData.props.props.children"
              placeholder="请输入子级键名"
            />
          </el-form-item>
        </template>
      </template>

      <template v-else>
        <draggable
          :list="activeData.options"
          :animation="340"
          group="selectItem"
          handle=".option-drag"
        >
          <div
            v-for="(item, index) in activeData.options"
            :key="index"
            class="select-item"
          >
            <div class="select-line-icon option-drag">
              <i class="el-icon-s-operation" />
            </div>
            <el-input v-model="item.label" placeholder="选项名" size="small" />
            <el-input
              placeholder="选项值"
              size="small"
              :value="item.value"
              @input="setOptionValue(item, $event)"
            />
            <div
              class="close-btn select-line-icon"
              @click="activeData.options.splice(index, 1)"
            >
              <i class="el-icon-remove-outline" />
            </div>
          </div>
        </draggable>
        <div style="margin-left: 20px">
          <el-button
            style="padding-bottom: 0"
            icon="el-icon-circle-plus-outline"
            type="text"
            @click="activeData.options.push({ label: '', value: '' })"
            >添加选项
          </el-button>
        </div>
      </template>
    </template>
  </div>
</template>

<script>
import draggable from "vuedraggable";
import cloneDeep from "lodash/cloneDeep";
import { isNumberStr } from "@/utils";

export default {
  name: "UnDSelectSetter",
  components: {
    draggable,
  },
  props: {
    activeData: Object,
  },
  data() {
    // 缓存操作的数据
    return {
      currDynamicData: [],
      currDynamicProps: {},
      currStaticData: [
        {
          label: "选项一",
          value: 1,
        },
        {
          label: "选项二",
          value: 2,
        },
      ],
    };
  },
  watch: {
    "activeData.__config__.renderKey": {
      handler(val, oldVal) {
        // 当前在切换不同的用户选择组件，需要重置选项
        if (val !== oldVal) {
          this.setOptions(this.activeData.options);
        }
      },
    },
    "activeData.options": {
      handler(val) {
        if (this.activeData.__config__.dataType === "dynamic") {
          this.currDynamicData = cloneDeep(val);
        } else {
          this.currStaticData = cloneDeep(val);
        }
      },
    },
    "activeData.__config__.dataType": {
      handler(val) {
        let options = [];
        let props = {};
        if (val === "dynamic") {
          options = this.currDynamicData;
          props = {
            ...this.activeData.props.props,
            ...this.currDynamicProps, // 恢复设置的键值
          };
        } else {
          options = this.currStaticData;
          // 修正前，保留动态数据时的设置
          this.currDynamicProps = {
            ...this.activeData.props.props,
          };
          // 然后再修正键值
          props = {
            ...this.activeData.props.props,
            label: "label",
            value: "value",
          };
        }
        this.$set(this.activeData, "options", options);
        this.$set(this.activeData.props, "props", props);
        this.$set(
          this.activeData.__config__,
          "defaultValue",
          this.activeData.multiple ? [] : ""
        );
      },
    },
    "activeData.props.props": {
      handler(val) {
        if (this.activeData.__config__.dataType === "dynamic") {
          this.currDynamicProps = val;
        }
      },
      deep: true,
    },
  },
  methods: {
    multipleChange(val) {
      this.$set(this.activeData.__config__, "defaultValue", val ? [] : "");
    },
    setOptionValue(item, val) {
      item.value = isNumberStr(val) ? +val : val;
    },
    setOptions(val) {
      if (this.activeData.__config__.dataType === "dynamic") {
        this.currDynamicData = cloneDeep(val);
      } else {
        const options = val.length
          ? val
          : [
              {
                label: "选项一",
                value: 1,
              },
              {
                label: "选项二",
                value: 2,
              },
            ];
        this.currStaticData = cloneDeep(val);
      }
    },
  },
};
</script>

<style lang="scss" scoped>
.close-btn {
  cursor: pointer;
  color: #f56c6c;
}
.select-item {
  display: flex;
  border: 1px dashed #fff;
  box-sizing: border-box;

  & .el-input + .el-input {
    margin-left: 4px;
  }
}
.select-item + .select-item {
  margin-top: 4px;
}
.select-item.sortable-chosen {
  border: 1px dashed #409eff;
}
.select-line-icon {
  line-height: 32px;
  font-size: 22px;
  padding: 0 4px;
  color: #777;
}
.option-drag {
  cursor: move;
}
</style>
